
/*
* Copyright 2009 AFCS Explorer http://code.google.com/p/afcs-explorer/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
* limitations under the License.
* 
* 
*  Acknowledgements:
 * The Base (AFCS) of this Code has been brought from CocomoSDK_0.91\extras\scripts\java\AFCS.java
 * All credits go to Adobe engineers who introduces AFCS.java
 * 
*/
package example.afcs;

import java.util.List;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ledtechdesign.afcs.AccountManager;
import com.ledtechdesign.afcs.IRole;
import com.ledtechdesign.afcs.ISession;
import com.ledtechdesign.beans.Account;
import com.ledtechdesign.beans.Item;
import com.ledtechdesign.service.ServiceManagerEnum;

public class AFCS {
	static Log logger = LogFactory.getLog("com.ledtechdesign.afcs.AFCS");
	public static void main(String args[]) throws Exception {
		String progname = "afcs";

		System.out.println(AFCS.class);
		String host = "http://connectnow.acrobat.com";
		int argc = 0;
		boolean useObject = false;

		while (argc < args.length) {

			String arg = args[argc];

			if (arg.startsWith("--host="))
				host = arg.substring(7);

			else if (arg.equals("--object"))
				useObject = true;
			else
				break;
			argc++;
		}

		if (args.length - argc < 2)
			usage(progname);

		String account = args[argc++];
		String username = args[argc++];
		String password = args[argc++];

		Account act = new Account(host, account);

		// AccountManager am = new AccountManager(act,
		// ServiceManagerEnum.OBJECT);
		 Properties props = new Properties();
		 props.setProperty("afcs.devUserName", username);
		 props.setProperty("afcs.devPassword", password);
		 	 
		AccountManager am = new AccountManager(props,
				act,
				(useObject ? ServiceManagerEnum.OBJECT : ServiceManagerEnum.XML));
		//Properties props = Utils.loadProperties();
		//am.account.setSecret(props.getProperty("afcs.devSecret"));
		am.login();

		if (args.length - argc == 0 || args[argc].equals("--list")) {
			logger.debug("==== template list for " + account + " ====");
			try {
				List<Item> templates = am.listTemplates();
				if (templates != null && templates.size() > 0) {
					for (Item aTemplate : templates) {
						logger.debug(aTemplate.getName() + " : "
								+ aTemplate.getCreated());
					}
				} else {
					logger.debug("No templates available");
				}
			} catch (Exception e) {
				logger.debug("error listing templates");
				e.printStackTrace(System.out);
			}
			logger.debug("==== room list for " + account + " ====");

			List<Item> rooms = am.listRooms();
			try {
				if (rooms != null && rooms.size() > 0) {
					for (Item aRoom : am.listRooms()) {
						logger.debug(aRoom.getName() + " : " + aRoom.getDesc()
								+ " : " + aRoom.getCreated());
					}
				} else {
					logger.debug("No rooms available");
				}
			} catch (Exception e) {
				logger.debug("error listing rooms");
				e.printStackTrace(System.out);
			}
		}

		else if (args[argc].equals("--create")) {
			String s = am.createRoom(args[argc + 1],
					(args.length - argc) > 2 ? args[argc + 2] : null)
					.toString();
			logger.debug("Create Result" + s);

		}

		else if (args[argc].equals("--delete")) {
			String s = am.deleteRoom(args[argc + 1]).toString();

			logger.debug("Delete Result" + s);
		}

		else if (args[argc].equals("--delete-template")) {
			String s = am.deleteTemplate(args[argc + 1]).toString();
			logger.debug("deleteTemplate Result" + s);
		}

		else if (args[argc].equals("--ext-auth")) {
			int role = IRole.LOBBY;

			if (args.length - argc >= 6) {
				String sRole = args[argc + 5];
				if (sRole.equalsIgnoreCase("none"))
					role = IRole.NONE;
				else if (sRole.equalsIgnoreCase("lobby"))
					role = IRole.LOBBY;
				else if (sRole.equalsIgnoreCase("viewer"))
					role = IRole.VIEWER;
				else if (sRole.equalsIgnoreCase("publisher"))
					role = IRole.PUBLISHER;
				else if (sRole.equalsIgnoreCase("owner"))
					role = IRole.OWNER;
				else
					role = Integer.parseInt(sRole);
			}

			ISession session = am.getSession(args[argc + 2]);
			String token = session.getAuthenticationToken(args[argc + 1],
					args[argc + 3], args[argc + 4], role).toString();
			logger.debug(token);
		}

		else if (args[argc].equals("--info")) {
			if ((args.length - argc) == 1)
				logger.debug(am.getAccountInfo());
			else
				logger.debug(am.getRoomInfo(args[argc + 1]));
		}

		else if (args[argc].equals("--get-nodes")) {
			logger.debug(am.getNodes(args[argc + 1], null));
		}

		else
			usage(progname);
	}

	private static void usage(String progname) {
		System.out.println("usage: " + progname
				+ " [--debug] [--host=url] account user password");
		System.out.println("         --list");
		System.out.println("         --create room [template]");
		System.out.println("         --delete room");
		System.out.println("         --info [room]");
		System.out
				.println("         --ext-auth secret room username userid role");
		System.exit(1);
	}
}
